GBML manual / Collision

tile_set_index

Sets the DRAWN tile index at (x, y) in the current room’s own tileset - which entry of the tileset sheet is pictured there, the same integer a room’s authored map already uses per cell. The drawing sibling of tile_set_class: paired together ("this cell no longer blocks" plus "this cell no longer looks solid"), the two are the whole of a permanent dig/destructible-terrain mechanic in one two-line call site, no extra instance needed.

Works differently on Game Boy - tile_set_index works as on the Advance with a limit: 32 changed squares of a room at once (more when the game names more squares with plain numbers, up to 255); a change that finds no free place is not drawn, and nothing tells you.

Works differently on Game Boy Color - tile_set_index works as on the Advance with a limit: 32 changed squares of a room at once (more when the game names more squares with plain numbers, up to 255); a change that finds no free place is not drawn, and nothing tells you.

On other consoles this differs: see Side by side and How the consoles differ.

Syntax

tile_set_index(x, y, index, layer = ROOM_LAYER_MAIN)

Arguments

NameType
xreal
yreal
indexint
layerroom_layer (can be left out; then it is ROOM_LAYER_MAIN)

Returns

Nothing.

Description

Sets the drawn tile index at (x, y) in the current room's own tileset - which entry of the tileset sheet is pictured there, the same integer a room's authored map already stores per cell. This is the drawing twin of tile_set_class: that one changes what a cell blocks, this one changes what it looks like. Paired together, they are the whole of a permanent dig/destructible-terrain mechanic in one small call site - "this cell no longer blocks" plus "this cell no longer looks solid" - with no extra instance placed on top and nothing left to collapse or evict later.

x and y are a place in the room, counted from the room's top-left corner. That is also where the tile grid starts, so the very first tile covers x from 0 to 7 and y from 0 to 7 (tiles are 8 by 8 pixels).

A different question from tile_anim_frame - worth telling the two apart precisely, because a project reasonably reaches for whichever one it heard of first. tile_anim_frame repaints a tile slot: every cell in the room that references that one hardware tile index changes together, everywhere, the instant it fires - same index, different pixels. tile_set_index repaints a cell: it changes which index that one cell references, leaving every other cell - including ones that used to share its old index - untouched, so it is the mirror image: same pixels, different index, at exactly one cell. An animated group is keyed by tile index, not by position, so it cannot single out one dug cell among hundreds sharing the same dirt tile without a separate authored tile index - and a separate copy of that tile's own art - per cell, which is absurd past a handful of cells. That is exactly the reason tile_anim_frame cannot do what this function does, and why digging (or anything else where one particular cell, and only that cell, should look different) wants tile_set_index instead.

The two compose freely, and neither needs to know about the other. If a tile_set_index call happens to name an index some tile_anim group is also overwriting, the cell simply shows that group's current animated frame, exactly like every other cell referencing the same index - this function only ever changes which index a cell points at; the animated tile system only ever changes the pixel data a shared index points at, in a completely separate part of VRAM. A dug tunnel that also happens to sit under an animated water tile needs nothing special from either side to work correctly.

Preserves the cell's horizontal/vertical flip exactly as authored (or as a previous tile_set_index call left them) and, for a tileset with one palette, its palette too - only the picture itself changes. When the tileset is drawn with several palettes, the cell takes the palette of its new tile. index is clamped to the valid tile-index range (0-1023); a value outside that range is a no-op.

A different no-op condition than tile_set_class's own - worth stating precisely rather than assuming it matches. This checks the room's drawn layer's own padded map cell count (rounded up to a multiple of 32 per axis - the Game Boy Advance module requires that for a background map) against this engine's live-mutable drawing budget, not the room's real, authored column/row count tile_set_class checks. Both are measured against the same max_mutable_tile_cells ceiling - what differs is which count is measured, so a padded map can be over it while the authored size is under. A room can therefore be over one budget and under the other - tile_index would then still read the room's real, unchanged picture; only the write no-ops. Same "does not persist" limit as tile_set_class: a change here is not saved and does not survive a room change, a room restart, or a save/load - the room's tile layer resets to its authored picture the next time it loads. A picture change that should stay is a collision layer: draw it in the Room Editor and switch to it with tile_layer_set_active. That lays the whole room down again, so a picture change you made with tile_set_index is lost when you switch to a different layer.

A tile layer other than Main. The optional last argument, layer, picks which of the room's tile layers to change: ROOM_LAYER_MAIN (the room's own tiles, and what you get when you leave the argument out), ROOM_LAYER_FRONT or ROOM_LAYER_BACK. Only the picture of that layer changes: collision stays on the Main layer, whichever layer you name. A room that does not have that layer does nothing. A Front or Back layer follows the same rules as Main (the same size limit, the same "does not persist" limit, a hidden layer is changed too). A game that never names a layer does not carry any of the extra code, and a Front or Back layer only keeps a picture the game can change in a game that names one.

Example

if (tile_solid(x + 8, y) && (tile_flags(x + 8, y) & 4) != 0) {
    tile_set_class(x + 8, y, TILE_PASS);
    tile_set_index(x + 8, y, 0);
    tile_set_index(x + 8, y, 0, ROOM_LAYER_BACK);
}

Digs the cell just ahead of the walker: if it is solid dirt (bit 2 of its flags marking it diggable, the same convention tile_set_flags's own example uses), the cell stops blocking movement and its picture switches to tile index 0 (this tileset's own open-floor tile) in the same two-line action - a complete, permanent dig, nothing left to place, track or evict. The last line does the same for a Back layer that shows a darker earth behind the room's own tiles.

Other consoles

On the Game Boy Color and the original Game Boy, a room keeps a list of the squares changed from the picture it was painted with. The list is as long as the number of different squares your code names with plain numbers, at least 32 (16 when only the class or flags change) and at most 255. A square put back to its painted tile gives its place back. A square your code works out while the game runs (from a variable or a loop) counts only toward the 32. A change that finds no free place is not drawn. On the Game Boy Color and the Game Boy, a game that changes more than 255 squares of a room sets Project Settings > Engine limits > Changed squares at once; up to the room's size is then kept.

On the GBA

Different question from tile_anim_frame, worth telling apart precisely: tile_anim_frame repaints a TILE SLOT - every cell in the room sharing that one hardware tile index changes together, everywhere, at once (same index, different pixels). tile_set_index repaints a CELL - it changes which index that ONE cell references, leaving every other cell (including ones that used to share its old index) untouched (same pixels, different index, one cell). An animated group cannot single out one dug cell among hundreds sharing the same dirt tile without a separate authored tile index - and a separate copy of that tile’s art - per cell, which is why tile_anim_frame cannot do what this does. The two compose freely: a cell whose tile_set_index names an index some tile_anim group is also overwriting simply shows that group’s current frame, like any other cell referencing the same index - a dug tunnel that also happens to be animated water needs nothing special from either side. Preserves the cell’s existing palette bank and h/v flip - only the picture changes. index is clamped to the valid tile-index range (0-1023); out of range is a no-op. A DIFFERENT no-op condition than tile_set_class’s own: this checks the room’s drawn layer’s own PADDED map cell count (rounded up to a multiple of 32 per axis - the GBA module requires that) against this engine’s live-mutable budget, not the room’s real, authored column/row count tile_set_class checks - so a room can be over one budget and under the other; tile_index would then still read the room’s real, unchanged picture. Same "does not persist across a room change/save-load" limit as tile_set_class. The optional last argument is the tile layer to change: ROOM_LAYER_MAIN (the default, the room’s own tiles), ROOM_LAYER_FRONT or ROOM_LAYER_BACK; collision stays on Main whichever layer is named, and a room with no such layer does nothing. A Front or Back layer is changed like Main (same padded-map budget, same “does not persist” limit). A game that never names a layer carries none of the extra code.

See also: tile_indextile_set_classtile_set_flagstile_anim_frameCollision layerstile_layer_set_activeroom_layer_visibleROOM_LAYER_FRONT